package com.mozmob.visa;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class VisaPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceClickListener {

	private PreferenceScreen cityPreference;
	
	private EditTextPreference notifyWhenPreference;
	
	public static final String KEY_ACTIVE_PREFERENCE = "activePreference";
	public static final String KEY_CITY_PREFERENCE = "cityPreference";
	public static final String KEY_CITY_PREFERENCE_BRASILIA = "cityPreferenceBrasilia";
	public static final String KEY_CITY_PREFERENCE_RECIFE = "cityPreferenceRecife";
	public static final String KEY_CITY_PREFERENCE_RIO_DE_JANEIRO = "cityPreferenceRioDeJaneiro";
	public static final String KEY_CITY_PREFERENCE_SAO_PAULO = "cityPreferenceSaoPaulo";
	public static final String KEY_NOTIFY_WHEN_PREFERENCE = "notifyPreference";
	
	public static final int CITY_BRASILIA = 0;
	public static final int CITY_RECIFE = 1;
	public static final int CITY_RIO_DE_JANEIRO = 2;
	public static final int CITY_SAO_PAULO = 3;
	
	public static boolean getPreferenceActive(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_ACTIVE_PREFERENCE, false);
	}

	public static boolean getPreferenceCityBrasilia(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_CITY_PREFERENCE_BRASILIA, false);
	}

	public static boolean getPreferenceCityRecife(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_CITY_PREFERENCE_RECIFE, false);
	}

	public static boolean getPreferenceCityRioDeJaneiro(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_CITY_PREFERENCE_RIO_DE_JANEIRO, false);
	}

	public static boolean getPreferenceCitySaoPaulo(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_CITY_PREFERENCE_SAO_PAULO, false);
	}

	public static int getPreferenceNotifyWhen(Context context) {
		return Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_NOTIFY_WHEN_PREFERENCE, "100"));
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Load the XML preferences file
		addPreferencesFromResource(R.xml.settings);
		
		 // Get a reference to the preferences
		this.cityPreference = (PreferenceScreen) findPreference(KEY_CITY_PREFERENCE);
		
		this.notifyWhenPreference = (EditTextPreference) findPreference(KEY_NOTIFY_WHEN_PREFERENCE);
		this.notifyWhenPreference.setOnPreferenceClickListener(this);
	}
	
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
        
		updateSelectedCities();

		int dayNotify = getPreferenceNotifyWhen(this);
		this.notifyWhenPreference.setSummary(dayNotify + " " + getResources().getString(R.string.days_label));
		
        // Set up a listener whenever a key changes            
		sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
	
    protected void onPause() {
        super.onPause();

        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
    }    

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(KEY_CITY_PREFERENCE)
        		|| key.equals(KEY_CITY_PREFERENCE_BRASILIA)
        		|| key.equals(KEY_CITY_PREFERENCE_RECIFE)
        		|| key.equals(KEY_CITY_PREFERENCE_RIO_DE_JANEIRO)
        		|| key.equals(KEY_CITY_PREFERENCE_SAO_PAULO)){
    		updateSelectedCities();
        }
    }	
    
	private void updateSelectedCities(){
		String summary = "";
		int countSelected = 0;
		
		if(getPreferenceCityBrasilia(this)){
			summary = getResources().getString(R.string.city_brasilia_label) + ", ";
			countSelected = countSelected + 1;
		}

		if(getPreferenceCityRecife(this)){
			summary = summary + getResources().getString(R.string.city_recife_label) + ", ";
			countSelected = countSelected + 1;
		}

		if(getPreferenceCityRioDeJaneiro(this)){
			summary = summary + getResources().getString(R.string.city_rio_de_janeiro_label) + ", ";
			countSelected = countSelected + 1;
		}

		if(getPreferenceCitySaoPaulo(this)){
			summary = summary + getResources().getString(R.string.city_sao_paulo_label) + ", ";
			countSelected = countSelected + 1;
		}
		
		if(summary.equals("")){
			summary = getResources().getString(R.string.city_noone_selected_label);
		} else if(countSelected == 4){
			summary = getResources().getString(R.string.city_all_selected_label);
		} else {
			summary = summary.substring(0, summary.length() - 2);
		}

		this.cityPreference.setSummary(summary);
		BaseAdapter adapter = (BaseAdapter) this.cityPreference.getRootAdapter();
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {

		if(preference.getKey().equals(KEY_NOTIFY_WHEN_PREFERENCE)){
			DayDialogAction dialogAction = new DayDialogAction();
			
			int numberOfDays = getPreferenceNotifyWhen(this);
			SeekBar seekBar = (SeekBar) this.notifyWhenPreference.getDialog().findViewById(R.id.seek_day);
			seekBar.setProgress(numberOfDays);
			seekBar.setOnSeekBarChangeListener(dialogAction);

			TextView textView = (TextView) this.notifyWhenPreference.getDialog().findViewById(R.id.text_day);
			textView.setText(numberOfDays + " " + getResources().getString(R.string.days_label));

			// Listener when to click on OK button
			AlertDialog alertDialog = (AlertDialog) this.notifyWhenPreference.getDialog();
			Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
			positiveButton.setOnClickListener(dialogAction);
			
		}
		
		return true;
	}
	
	class DayDialogAction implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			TextView textView = (TextView) notifyWhenPreference.getDialog().findViewById(R.id.text_day);
			textView.setText(progress + " " + getResources().getString(R.string.days_label));
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onClick(View view) {
			SeekBar seekBar = (SeekBar) notifyWhenPreference.getDialog().findViewById(R.id.seek_day);
			int day = seekBar.getProgress();
			notifyWhenPreference.setText("" + day);
			notifyWhenPreference.setSummary(day + " " + getResources().getString(R.string.days_label));
			notifyWhenPreference.getDialog().dismiss();
		}
	}
	
}